<?php

// pentru upload stocuri

	
if(isset($_FILE)){

		

	$handle = fopen($_FILES['filename']['tmp_name'], "r");
	 	
	    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
	    					
	      		 $shopObj->execute_query("UPDATE stocks SET quantity='{$data[1]}',price='{$data[2]}' WHERE barcode='{$data[0]}'"); 
	      		   
	      		}


}
		
$products = $shopObj->fetch_result("SELECT products.*,products_details.name,products_details.url FROM products,products_details WHERE products.id= products_details.product_id AND products_details.language_id='{$language[0]['id']}' ");
		
?>

		<div id='table_products_wrapper'>
			<a href='<?php echo SERVER_PATH."product/new.html" ?>' class='btn btn-primary btn-success'>+<?php  echo translate_word_shop('Add product',$dictionary); ?></a>
			<button class='btn btn-primary btn-success' onclick='open_modal();'>+<?php  echo translate_word_shop('import_stocks',$dictionary); ?></button>
				<table id="products" class="table table-striped table-bordered table-hover">
					<thead>
						<tr>	
							<th><a href='javascript:void(0)' onclick='delete_selected()'><?php  echo translate_word_shop('Delete selected',$dictionary); ?></a></th>										
							<th><?php  echo translate_word_shop('Product',$dictionary); ?></th>
							<th><?php  echo translate_word_shop('Inventory',$dictionary); ?></th>
							<th><?php  echo translate_word_shop('Category',$dictionary); ?></th>
							<th><?php  echo translate_word_shop('Type',$dictionary); ?></th>
							<th><?php  echo translate_word_shop('Has discount',$dictionary); ?></th>
							<th><?php  echo translate_word_shop('Has liquidation',$dictionary); ?></th>
							<th><?php  echo translate_word_shop('Visible',$dictionary); ?></th>
							<th><?php  echo translate_word_shop('Actions',$dictionary); ?></th>
						</tr>
					</thead>

					<tbody>
						<?php 

							foreach ($products as $key => $value) {
								$nr_stock =$shopObj->fetch_one("SELECT SUM(quantity) FROM  stocks WHERE product_id = '{$value['id']}'");
								$categ = $shopObj->fetch_result("SELECT category_details.name FROM category_details 
																JOIN categories ON categories.id = category_details.category_id
																JOIN products_categories ON products_categories.category_id = categories.id
																WHERE products_categories.product_id = '{$value['id']}'  AND category_details.language_id='{$language['0']['id']}'");
								$categs=array();
								foreach($categ as $key_cat=>$value_cat){
									$categs[]=$value_cat['name'];
								}
								$categories = implode(', ', $categs);
								if($value['type']==2){
									$comm_s= "selected='selected'";
									$new_prod='';
								}else if($value['type']==1){
									$new_prod="selected='selected'";
									$comm_s='';
								}else{
									$none = "selected='selected'";
									$comm_s='';
									$new_prod='';
								}

								if($value['has_discount']==1){
									$discount = "checked='checked'";
								}else{
									$discount ='';
								}

								if($value['has_liqvidation']==1){
									$liquidation = "checked='checked'";
								}else{
									$liquidation ='';
								}

								if($value['visible']==1){
									$visible = "checked='checked'";
								}else{
									$visible ='';
								}
								echo "<tr>
										<td>
											<input type ='checkbox' value='{$value['id']}' class='delete_selected'>
											<span class='lbl'></span>
										</td>
										<td><a href ='edit_product.html?product_id={$value['id']}'>{$value['name']}</td>
										<td>{$nr_stock[0]}</td>
										<td>{$categories}</td>
										<td>
											<select onchange='change_type(this.value,{$value['id']})'>
												<option value='0'>none</option>
												<option value='1' {$new_prod}>".translate_word_shop('New product',$dictionary)."</option>
												<option value='2' {$comm_s}>".translate_word_shop('Comming soon',$dictionary)."</option>
											</select>
										</td>
										<td>
											<input type ='checkbox' onclick ='check_product(\"has_discount\",{$value['id']},this)' {$discount}>
											<span class='lbl'></span>
										</td>
										<td><input type ='checkbox' onclick ='check_product(\"has_liqvidation\",{$value['id']},this)' {$liquidation}><span class='lbl'></span></td>
										<td><input type ='checkbox' onclick ='check_product(\"visible\",{$value['id']},this)' {$visible}><span class='lbl'></span></td>
										<td>
												<a class='green' href='".SERVER_PATH."/product/{$value['url']}.html'>
										<i class='icon-pencil bigger-130'></i>
									</a>

									<a class='red' href='javascript:void(0)' onclick='delete_product({$value['id']},this)'>
										<i class='icon-trash bigger-130'></i>
									</a>

									
										</td>
									 </tr>";
							}

						 ?>
						


					</tbody>
				</table>
			</div>
			<div id="modal-import" class="modal hide fade"  tabindex="-1">
				<div class="modal-header no-padding">
					<div class="table-header">
						<button class="close" data-dismiss="modal" type="button">×</button>
						<?php echo translate_word_shop('Import stocks',$dictionary); ?>
					</div>		
					<form metho='POST' action='http://www.neo-flux.com/wizard/products.html' name='csv_form' id='csv_form' enctype="multipart/form-data">			
						<input type="file" name="file_upload_pics" id="file_upload_pics" />
						<!-- <input type='text' namame ='mergi'> -->
						<!-- <input type='submit' class="btn btn-small btn-danger btn-primary"  value='Save' > -->
					</form>
				</div>	
				<br class='clear'/>
				<div class="modal-footer ">					
					<button class="btn btn-small btn-danger btn-primary" data-dismiss="modal" onclick = "$('#csv_form').submit();">Save</button>
					<button class="btn btn-small btn-danger btn-primary" data-dismiss="modal">Cancel</button>
				</div>
				
			</div>